import { searchsg, recomrbo, getmusic, getgedan, getgdlist, gethotlist, getcatelist, getrank, getranklist, getsongs, getnewmv, getmedio, getrecmv, getrankmv,getnewmusic, getallmusic } from "@/api";

const state = {
   searchsg: [],
   //轮播图存储区
   recomlbo: [],
   getgedan: [],
   //歌单分类储存区
   getgd: [],
   getgdlist: [],
   gethotlist: [],
   getcatelist: [],
   //前四个
   getrank: [],
   //后面的
   getrank2: [],
   //歌单列表
   getranklist: [],
   getsongs: [],
   //newmv储存区
   getnewmv: [[], [], []],
   musicurl:'',
   mvurl: '',
   //热门新歌存储
   getnewmusic:[],
   getallmusic:{}
};
const mutations = {
   SEARCHSG(state, name) {
      state.searchsg = []
      name.forEach(item => {
         let sog = { 'value': '', 'name': '' }
         //console.log(sog);
         sog.value = item.name,
            sog.name = item.artist.name,
            state.searchsg.push(sog)
      })
      //console.log(state.searchsg);
   },
   RECOMLBO(state, res) {
      state.recomlbo = res
      // res.forEach(item => {
      //    let sog = { 'imgurl': '', 'id': '',name:'' }
      //    //console.log(sog);
      //    sog.imgurl = item.picUrl,
      //       sog.id = item.id,
      //       sog.name=item.name
      //       state.recomlbo.push(sog)
      // })
    //console.log(state.recomlbo);
   },
   GETGEDAN(state, res) {
      state.getgedan = JSON.parse(JSON.stringify(res))
         
      //console.log(state.getgedan);
   },
   GETGDLIST(state, res) {
      state.getgd = res.categories;
      state.getgdlist = [[], [], [], [], [], []];
      res.sub.forEach(item => {

         state.getgdlist[item.category].push(item.name)
      })
      console.log(state.getgdlist);
      //console.log(state.getgd);
   },
   GETHOTLIST(state, res) {
      state.gethotlist = []
      res.forEach(item => {
         state.gethotlist.push(item.name)
      })
      console.log(state.gethotlist);
   },
   GETCATLIST(state, res) {
      state.getcatelist = []
      res.forEach(item => {
         let so = { coverImgUrl: '', name: '',id:'' }
         so.coverImgUrl = item.coverImgUrl
         so.name = item.name
         so.id = item.id
         state.getcatelist.push(so)
      })
      console.log(state.getcatelist);
   },
   GETRANK(state, res) {
      state.getrank = []
      state.getrank2 = []
      let s = 0
      res.forEach(item => {
         let so = { coverImgUrl: '', name: '', id: '', updateTime: '' }

         so.coverImgUrl = item.coverImgUrl;
         so.name = item.name;
         so.id = item.id
         so.updateTime = item.updateTime
         if (s < 4) {
            state.getrank.push(so)
         } else {
            state.getrank2.push(so)
         }
         s++
      })
      //console.log(state.getrank);
      // console.log(state.getrank2);
   },
   GETRANKLIST(state, res) {
      //console.log( state.getranklist);
      //console.log(m);
  // console.log(res.songs);
      if (state.getranklist.length == state.getrank.length) {
         state.getranklist = []
      }
      let getranklists = []
      res.songs.forEach(item => {

         let so = { name: '', arname: '', id: '' }
         so.name = item.name;
         so.id = item.id
         so.arname = item.ar[0].name
         getranklists.push(so)


      })
      state.getranklist.push(getranklists)
      // console.log(state.getranklist);


   },
   GETSONGS(state, res) {
      state.getsongs = []
      res.forEach(item => {
         let so = { name: '', img1v1Url: '', id: '' }
         so.name = item.name;
         so.id = item.id
         so.img1v1Url = item.img1v1Url
         state.getsongs.push(so)
      })
      console.log(state.getsongs);
      //sessionStorage.setItem('songsimg',JSON.stringify(state.getsongs))
   },
   GETNEWMV(state, res) {
      state.getnewmv[0].splice(0)
      res.forEach(item => {
         let so = { id: '', artistId: '', artistName: '', cover: '', name: '', playCount: '' }
         so.id = item.id
         so.artistId = item.artistId
         so.artistName = item.artistName
         so.cover = item.cover
         so.name = item.name
         so.playCount = Math.ceil(item.playCount / 10000)
         state.getnewmv[0].push(so)
      })
      //console.log(state.getnewmv);
   },
   GETMEDIO(state, res) {
      state.mvurl = res.url
   },
   GETRECMV(state, res) {
      state.getnewmv[1].splice(0);
      res.forEach(item => {
         let so = { id: '', artistId: '', artistName: '', cover: '', name: '', playCount: '' }
         so.id = item.id
         so.artistId = item.artistId
         so.artistName = item.artistName
         so.cover = item.picUrl
         so.name = item.name
         so.playCount = Math.ceil(item.playCount / 10000)
         state.getnewmv[1].push(so)
      })
      console.log(state.getnewmv)
   },
   GETRANKMV(state, res) {
      state.getnewmv[2].splice(0);
      res.forEach(item => {
         let so = { id: '', artistId: '', artistName: '', cover: '', name: '', playCount: '' }
         so.id = item.id
         so.artistId = item.artistId
         so.artistName = item.artistName
         so.cover = item.cover
         so.name = item.name
         so.playCount = Math.ceil(item.playCount / 10000)
         state.getnewmv[2].push(so)
      })
      console.log(state.getnewmv);
   },
   GETNEWMUSIC(state,res){
              state.getnewmusic.splice(0)
              res.forEach(item=>{
                   let so ={name:'',picUrl:'',id:'',artists:[],}
                   so.name=item.name,
                   so.id=item.id,
                   so.picUrl=item.picUrl,
                   so.artists=JSON.parse(JSON.stringify(item.artists))
                   state.getnewmusic.push(so)
              })
              //console.log(state.getnewmusic);
   },
   GETALLMUSIC(state,res){
        
       state.getallmusic=JSON.parse(JSON.stringify(res))
       console.log(state.getallmusic);
   },
   getdanurl(state,res){
      state.musicurl=''
      setTimeout(()=>{
         state.musicurl=res
      },500)
   }
     
};
const actions = {
   async searchsg({ commit }, keyword) {
      const res = await searchsg(keyword);
      //console.log(res);
      if (res.code == 200) {
         commit('SEARCHSG', res.result.albums)
      }
   },
   //轮播图片区
   async recomlbo({ commit }) {
      const res = await recomrbo();
        //console.log(res);
      if (res.code == 200) {
         commit('RECOMLBO', res.result)
      }
   },
   //获取音乐区
   async getmusic({ commit }, id) {
      const res = await getmusic(id)
     console.log(res);
      if (res.code == 200) {
         //console.log('111');
         commit('getdanurl', res.data[0].url)
        
      }
   },
   //歌单区
   async getgedan({ commit }) {
      const res = await getgedan();
      //console.log(res);
      if (res.code == 200) {
         commit('GETGEDAN', res.result)
      }
   },
   //歌单分类名储存区
   async getgdlist({ commit }) {
      const res = await getgdlist();
      //console.log(res);
      if (res.code == 200) {
         commit('GETGDLIST', res)
      }
   },
   //热门歌单分类区
   async gethotlist({ commit }) {
      const res = await gethotlist();
      console.log(res);
      if (res.code == 200) {
         commit('GETHOTLIST', res.tags)
      }
   },
   //分类歌单区
   async getcatelist({ commit }, { m, mi }) {
      // console.log(m);
      const res = await getcatelist(m, mi);
       console.log(res);
      if (res.code == 200) {
         commit('GETCATLIST', res.playlists)
      }
   },
   //歌单排行区
   async getrank({ commit }) {
      const res = await getrank();
      //console.log();
      //console.log(res);
      if (res.code == 200) {
         commit('GETRANK', res.list)
      }
   },
   //歌单排行榜详情区
   async getranklist({ commit }, m) {
      // console.log(m);

      const res = await getranklist(m);
      //console.log(res)

      if (res.code == 200) {
         commit('GETRANKLIST', res)
      }
   },
   //歌手分类区
   async getsongs({ commit }, { m, mi, ms, mq }) {
      //console.log(m,mi,ms,mq);
      const res = await getsongs(m, mi, ms, mq);
      console.log(res);
      if (res.code == 200) {
         commit('GETSONGS', res.artists)
      }
   },
   //最新mv区
   async getnewmv({ commit }, m) {
      const res = await getnewmv(m);
      //console.log(res);
      if (res.code == 200) {
         commit('GETNEWMV', res.data)
      }
   },
   //mv地址区
   async getmedio({ commit }, m) {
      const res = await getmedio(m)
      //console.log(res);
      if (res.code == 200) {
         commit('GETMEDIO', res.data)
      }
   },
   //推荐mv区
   async getrecmv({ commit }) {
      const res = await getrecmv();
      // console.log(res);
      if (res.code == 200) {
         commit('GETRECMV', res.result)
      }
   },
   //mv排行榜
   async getrankmv({ commit },m) {
      const res = await getrankmv(m);
     // console.log(res);
      if (res.code == 200) {
         commit('GETRANKMV', res.data)
      }
   },
   //最新歌曲
   async getnewmusic({commit}){
      const res =await  getnewmusic();
     
     console.log(res);
     res.albums.splice(10)
      if(res.code==200){
           commit('GETNEWMUSIC',res.albums)
      }
   },
   //全部歌曲
   async getallmusic({commit},{m,mi}){
       const res = await getallmusic(m,mi);
       console.log(res);
       if(res.code==200){
          commit('GETALLMUSIC',res)
       }
   }
};
const getters = {};
export default {
   state,
   mutations,
   actions,
   getters
}